//
//  DetailDCaseVC.m
//  HomeFurnishing
//
//  Created by dllo on 16/3/31.
//  Copyright © 2016年 YMing. All rights reserved.
//

#import "DetailDCaseVC.h"
#import "DetailCaseTableViewCell.h"
@interface DetailDCaseVC ()<UITableViewDelegate,UITableViewDataSource>
@property (retain, nonatomic) IBOutlet UIImageView *uesrImgView;
@property (retain, nonatomic) IBOutlet UIImageView *imgView;
@property (retain, nonatomic) IBOutlet UILabel *user;
@property (retain, nonatomic) IBOutlet UILabel *titlel;
@property (retain, nonatomic) IBOutlet UILabel *style;
@property (retain, nonatomic) IBOutlet UILabel *area;
@property (retain, nonatomic) IBOutlet UILabel *cost;
@property (retain, nonatomic) IBOutlet UILabel *type;
@property (retain, nonatomic) IBOutlet UILabel *address;
@property (nonatomic, retain) UITableView *tv;
@property (nonatomic, strong) UIView *statusView;
@property (retain, nonatomic) NSMutableArray *dataArr;
@property (nonatomic, strong) UIView *naviView;
@property (nonatomic, retain) NSArray *sectionArr;
@property (nonatomic, retain) NSMutableArray *imgArr;
@end

@implementation DetailDCaseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createTableView];
    self.naviView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    
    [self.navigationController.view addSubview:_naviView];
    self.navigationController.navigationBarHidden = YES;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backBarButtonAction)];
    self.uesrImgView.layer.cornerRadius = self.uesrImgView.frame.size.width/2;
    self.uesrImgView.clipsToBounds = YES;
    self.sectionArr = [NSArray array];
    self.imgArr = [NSMutableArray array];
    self.dataArr = [NSMutableArray array];
    [self viewDataHandle];
    
}
- (void)backBarButtonAction
{
    [self.naviView removeFromSuperview];
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController popViewControllerAnimated:YES];
}
/**
 *  view数据解析
 */
-(void)viewDataHandle
{
    //NSLog(@"%@",_model.styleShow);
    self.style.text = _model.styleShow;
    self.type.text = _model.typeShow;
    self.area.text = [NSString stringWithFormat:@"%@平米",_model.areaShow];
    self.cost.text = [NSString stringWithFormat:@"%@万元",_model.cost];
    NSString *str = [NSString stringWithFormat:@"http://gooju.cn/dimages/%@_0_w750_h398_m0.jpg",_model.coverPhoto];
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:str]];
    self.user.text = _model.userName;
    self.titlel.text = _model.title;
    [self.uesrImgView sd_setImageWithURL:[NSURL URLWithString:_model.small]];
    
    NSString *strr = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/project/%@/v2?userId=0",self.mId];
    [NetworkHandle GetDataWithURL:strr dic:nil success:^(id responseObject) {
        self.dataArr = [CaseDBaseModel transformWithArray:responseObject[@"taskRun"][@"space"]];
        [_tv reloadData];
    } filed:^(NSError *error) {
        
    }];
}
-(void)createTableView
{
    self.tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT -64) style:(UITableViewStylePlain)];
    
    _tv.delegate = self;
    _tv.dataSource = self;
    [self.view addSubview:_tv];
    _tv.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    [_tv release];
    [_tv registerClass:[DetailCaseTableViewCell class] forCellReuseIdentifier:@"DetailCaseVCCell"];
    UIView *view = [[NSBundle mainBundle] loadNibNamed:@"DHeaderView" owner:self options:nil].lastObject;
    view.frame = CGRectMake(0, 0, SCREEN_WIDTH, 300);
    self.tv.tableHeaderView = view;
}



//分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CaseDBaseModel *m = _dataArr[section];
    return m.img.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailCaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCaseVCCell" forIndexPath:indexPath];
        CaseDBaseModel *m = _dataArr[indexPath.section];
        NSDictionary *dic = m.img[indexPath.row];
        NSString *str = [NSString stringWithFormat:@"http://gooju.cn/dimages/%@_0_w750_h0_m0.jpg",dic[@"photoId"]];
        [cell.imgView sd_setImageWithURL:[NSURL URLWithString:str]];
    return cell;
}
//分区头部字符串
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    CaseDBaseModel *m = self.dataArr[section];
    return m.name;
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CaseDBaseModel *m = self.dataArr[indexPath.section];
    NSDictionary *dic = m.img[indexPath.row];
    CGFloat h = [dic[@"height"] floatValue] *SCREEN_WIDTH /[dic[@"width"] floatValue];
    return h;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 滚动方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //NSLog(@"%f",self.tv.contentOffset.y);
    // 判断tableView滚动
    if (scrollView == _tv) {
        // 计算透明度
        CGFloat alpha = scrollView.contentOffset.y / 64;
        _naviView.backgroundColor = [UIColor colorWithRed:0 green:253/255.0 blue:254/255.0 alpha:alpha];
        if (scrollView.contentOffset.y > 100) {
            self.navigationController.navigationBarHidden = NO;

        }else{
            self.navigationController.navigationBarHidden = YES;
        }
        
    }
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)dealloc {
    [_uesrImgView release];
    [_imgView release];
    [_user release];
    [_titlel release];
    [_style release];
    [_area release];
    [_cost release];
    [_type release];
    [_address release];
    [_tv release];
    [_statusView release];
    [_dataArr release];
    [_sectionArr release];
    [_imgArr release];
    [_mId release];
    [_model release];
    [super dealloc];
}
@end
